package main

import (
	"encoding/xml"
	"io/ioutil"
	"strconv"
)

type xmlProperty struct {
	Name  string `xml:"name,attr"`
	Value string `xml:",chardata"`
}

type xmlLoggerConfig struct {
	Type     string        `xml:"type"`
	Property []xmlProperty `xml:"property"`
}

func(xml *xmlLoggerConfig)GetType() string{
	return xml.Type
}


func(xml *xmlLoggerConfig)GetFileName() string{
	for _, prop := range xml.Property {
		if prop.Name == "fileName" {
			return prop.Value
		}
	}
	return ""
}

func(xml *xmlLoggerConfig)GetFilePath() string{
	for _, prop := range xml.Property {
		if prop.Name == "filePath" {
			return prop.Value
		}
	}
	return ""
}

func(xml *xmlLoggerConfig)GetChannelSize() int{
	for _, prop := range xml.Property {
		if prop.Name == "channelSize" {
			res, err := strconv.Atoi(prop.Value)
			if err != nil {
				res = 50000
			}
			return res
		}
	}
	return 50000
}

func(xml *xmlLoggerConfig)GetSplitType() string{
	for _, prop := range xml.Property {
		if prop.Name == "splitType" {
			return prop.Value
		}
	}
	return "hour"
}

func(xml *xmlLoggerConfig)GetSplitSize() int64{
	if xml.GetSplitType() == "size" {
		for _, prop := range xml.Property {
			if prop.Name == "channelSize" {
				res, err := strconv.ParseInt(prop.Value, 10, 64)
				if err != nil {
					res = 104857600
				}
				return res
			}
		}
	}
	return 104857600
}

func LoadConfiguration(filename string, v interface{}) error{
	if content, err := ioutil.ReadFile(filename); err != nil {
		return err
	} else {
		if err := xml.Unmarshal(content, v); err != nil {
			return err
		}
		return nil
	}
}
